package tests;

import linkcut.AuxVertex;
import linkcut.LinkCutTree;

import org.junit.Assert;
import org.junit.Test;

public class LinkCutTests {

	@Test
	public void singleIsRoot() {
		LinkCutTree<Integer> tree = new LinkCutTree<Integer>();
		AuxVertex<Integer> v = tree.makeTree(null);
		Assert.assertEquals(v, tree.findRoot(v));
	}
	
	@Test
	public void simpleLink() {
		LinkCutTree<Integer> tree = new LinkCutTree<Integer>();
		AuxVertex<Integer> v = tree.makeTree(null);
		AuxVertex<Integer> w = tree.makeTree(null);
		tree.link(v, w);
		Assert.assertEquals(tree.findRoot(v), tree.findRoot(w));
	}
	
	@Test
	public void biggerLink() {
		LinkCutTree<Integer> tree = new LinkCutTree<Integer>();
		AuxVertex<Integer> v = tree.makeTree(1);
		AuxVertex<Integer> w = tree.makeTree(2);
		AuxVertex<Integer> y = tree.makeTree(3);
		tree.link(v, w);
		tree.link(y, w);
		Assert.assertEquals(tree.findRoot(y).getValue(), tree.findRoot(v).getValue());
	}
	
	@Test
	public void cut() {
		LinkCutTree<Integer> tree = new LinkCutTree<Integer>();
		AuxVertex<Integer> v = tree.makeTree(null);
		AuxVertex<Integer> w = tree.makeTree(null);
		tree.link(v, w);
		tree.cut(v);
		Assert.assertNotEquals(tree.findRoot(v), tree.findRoot(w));
	}
	
	@Test
	public void cutSingle() {
		LinkCutTree<Integer> tree = new LinkCutTree<Integer>();
		AuxVertex<Integer> v = tree.makeTree(null);
		tree.cut(v);
		Assert.assertEquals(v, tree.findRoot(v));
	}
	
	@Test
	public void cutRoot() {
		LinkCutTree<Integer> tree = new LinkCutTree<Integer>();
		AuxVertex<Integer> v1 = tree.makeTree(1);
		AuxVertex<Integer> w2 = tree.makeTree(2);
		tree.link(v1, w2);
		tree.cut(w2);
		Assert.assertEquals(tree.findRoot(v1), tree.findRoot(w2));
	}
}
